package com.example.ec.controller;

import com.example.ec.model.User;
import com.example.ec.service.GoodsService;
import com.example.ec.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserController extends BaseController{

    private final UserService userService;
    private final GoodsService goodsService;

    public UserController(UserService userService, GoodsService goodsService) {
        this.userService = userService;
        this.goodsService = goodsService;
    }

    @PostMapping("create")
    private String create(User user){
        userService.create(user);
       return "redirect:/index.jsp";

    }
    @PostMapping("queryUsernameAndPassword")
    private String queryUsernameAndPassword(User user){

       user= userService.queryOne("queryUsernameAndPassword",user);
        if(user==null){

            session.setAttribute("error","Invalid username or password");
            return "redirect:/index.jsp";
        }if(user.getRole()==1){

            return "redirect:/admin/index.jsp";

        }else {
               session.setAttribute("goodss",goodsService.queryAll());
            return "redirect:/user/index.jsp";
        }

    }
}
